package com.shanghaionstar.utils;

import android.content.Context;
import android.util.SparseArray;

import com.onstar.cn.common.exception.HttpException;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.http.CommonError;
import com.onstar.cn.framework.utils.GsonHelper;
import com.shanghaionstar.report.bean.DynamicDTO;
import com.shanghaionstar.report.bean.ReportReqParam;
import com.shanghaionstar.report.model.ReportModel;

import java.util.LinkedHashMap;

/**
 * Created by 6335yz on 2016/7/7.
 */
public class NetConnectUtil {
    public static String getResponse(SparseArray<String> reseponseData) throws HttpException {
        if (reseponseData != null && reseponseData.size()!=0) {
            int responseCode = -1;
//            for (Integer key : reseponseData.keySet()) {
//                responseCode = key;
//                break;
//            }

            for(int i=0;i<reseponseData.size();i++) {
                responseCode = reseponseData.keyAt(i);
                break;
            }
            if (responseCode != 200 && responseCode != 202) {
                CommonError commonError = GsonHelper.fromJson(reseponseData.get(responseCode),CommonError.class);
                throw new HttpException(commonError.getErrorCode(), commonError.getErrorMessage());
            } else {
                return reseponseData.get(responseCode);
            }
        }
        return null;
    }

    public static RequestEntity getAuthRequestEntity(Context context) {
        RequestEntity requestEntity = new RequestEntity();
        requestEntity.setReportSender(BusinessUtil.class);
        DynamicDTO dynamicDTO = new ReportModel().getDynamicDTO(context, ReportReqParam.getURLReqParam());
        requestEntity.setReport(dynamicDTO);
        LinkedHashMap<String, Object> customHeads = new LinkedHashMap<String, Object>();
        customHeads.put(FinalConstants.HTTP_AUTHORIZATION, ApplicationUtil.getInstance().getToken());
        requestEntity.setCustomHeads(customHeads);
        return requestEntity;
    }
}
